<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="java.util.LinkedList"%>
<%@ page import="java.util.HashMap"%>
<%@ page import="java.util.Enumeration"%>

<%
class TurnPage {
	LinkedList<HashMap<String,String>> userVisitUrl = null;
	private String GdUserVisitUrlList = "KdUserVisitUrlList";
	HttpServletRequest request = null;
	HttpSession session = null;

	public TurnPage(HttpServletRequest request,HttpSession session) {
		Object obj = session.getAttribute(GdUserVisitUrlList);
		if(obj!=null){
			userVisitUrl = (LinkedList<HashMap<String,String>>)obj;
		}else{
			userVisitUrl = new LinkedList<HashMap<String,String>>();
		}
		this.request = request;
		this.session = session;
	}

	public void addUrl() {
		HashMap<String,String> tpHash = new HashMap<String,String>();
		String preVisitUrl = request.getHeader("Referer");
		preVisitUrl = UrlPage(preVisitUrl, 0);

		Enumeration en = request.getParameterNames();
		while(en.hasMoreElements()){
		    String el = String.valueOf(en.nextElement());
		    String elValue = String.valueOf(request.getParameter(el));
		    elValue = elValue == null ? "" : elValue;
		    tpHash.put(el,elValue);
		}
		tpHash.put("url",preVisitUrl);
		userVisitUrl.addLast(tpHash);
		//System.out.println("tpHash="+userVisitUrl.size()+"="+tpHash);
		session.setAttribute(GdUserVisitUrlList,userVisitUrl);
	}

	public HashMap<String,String> getPreUrl() {
		HashMap<String,String> tpHash = null;
		if(userVisitUrl.size()>0) {
			tpHash = userVisitUrl.get(userVisitUrl.size()-1);
		}
		return tpHash;
	}

	public HashMap<String,String> removePreUrl() {
		HashMap<String,String> tpHash = null;
		if(userVisitUrl.size()>0) {
			tpHash = userVisitUrl.removeLast();
			session.setAttribute(GdUserVisitUrlList,userVisitUrl);
		}
		return tpHash;
	}

	public void removeAll() {
		userVisitUrl.clear();
		session.setAttribute(GdUserVisitUrlList,userVisitUrl);
	}

	/**
	*判断用户是否按返回到当前页
	*面的//非null代表是按了返回，null代表不是
	*/
	public HashMap<String,String> isPreUrl(String requestVal) {
		if(requestVal != null && !"".equals(requestVal) && !"null".equals(requestVal)){
			addUrl();
			String curUri = request.getRequestURI();
			for(int i=0; i<userVisitUrl.size(); i++){
				String flag = "yes"; 
				if(requestVal.indexOf(",")>0) flag = requestVal.split(",")[1];
				if(userVisitUrl.get(i).get("url").indexOf(curUri) != -1 && flag == "yes"){
					userVisitUrl.remove(i);
					break;
				}
			}
            return null;
        }else{
        	HashMap<String,String> tpHash = getPreUrl();
        	removePreUrl();
        	return tpHash;
        }
	}

	//各级页面存取参数
	public String getValue(HashMap<String,String> tpHash, String valKey, String requestVal, String defaultVal){
		String retuVal = "";
		if(valKey != null && valKey.equals("url")){
			tpHash = getPreUrl();
			if(tpHash != null){
				retuVal = tpHash.get(valKey);
			}
		}else if(requestVal != null && !"".equals(requestVal) && !"null".equals(requestVal)){
			retuVal = requestVal;
		}else if(tpHash != null){
			retuVal = tpHash.get(valKey);
		}

		if(retuVal == null || "".equals(retuVal) || "null".equals(retuVal)){
			if(defaultVal != null && !"".equals(defaultVal) && !"null".equals(defaultVal)){
				retuVal = defaultVal;
			}else if(valKey != null && valKey.equals("url")){
				retuVal = "index.jsp";
			}
		}
		return retuVal;
	}

	//首页取参数
	public String getPortalId(HashMap<String,String> tpHash,String valKey,String defaultVal){
		String retuVal = "";
		if(tpHash != null){
			retuVal = tpHash.get(valKey);
		}
		if(retuVal == null || "".equals(retuVal) || "null".equals(retuVal)){
			retuVal = defaultVal;
		}
		removeAll();
		return retuVal;
	}

	//第三方跳转回去取上级页面
	public String getReturnUrl(String valKey){
		HashMap<String,String> tpHash = getPreUrl();
		String retuVal = "";
		if(tpHash != null){
			retuVal = tpHash.get(valKey);
		}
		if(retuVal == null || "".equals(retuVal) || "null".equals(retuVal)){
			retuVal = "index.jsp";
		}
		return retuVal;
	}

	public String UrlPage(String strURL, int _index) {
		String strPage=null;
		String[] arrSplit=null;
		if(strURL != null){
			strURL=strURL.trim();
			if(strURL.indexOf("?") != -1){
				arrSplit=strURL.split("\\?");
				if(strURL.length()>_index) {
					strPage=arrSplit[_index];
				}
			}else{
				strPage = strURL;
			}
		}
		return strPage;
	}
}
%>